# Options that control whether to use system dependencies or build them from source,
# and whether to link them statically.
include(functions/dependency_options)
include(functions/emscripten_system_library)

if(EMSCRIPTEN)
  emscripten_system_library("zlib" ZLIB::ZLIB USE_ZLIB=1)
else()
  dependency_options("zlib" DEVILUTIONX_SYSTEM_ZLIB ON DEVILUTIONX_STATIC_ZLIB)
  if(NOT DEVILUTIONX_SYSTEM_ZLIB)
    add_subdirectory(3rdParty/zlib)
  endif()
endif()

if(SUPPORTS_MPQ)
  # bzip2 is a libmpq dependency.
  if(EMSCRIPTEN)
    emscripten_system_library("bzip2" BZip2::BZip2 USE_BZIP2=1)
  else()
    dependency_options("bzip2" DEVILUTIONX_SYSTEM_BZIP2 ON DEVILUTIONX_STATIC_BZIP2)
    if(NOT DEVILUTIONX_SYSTEM_BZIP2)
      add_subdirectory(3rdParty/bzip2)
    endif()
  endif()
endif()

find_package(Lua 5.4 QUIET)
if(LUA_FOUND)
  message("-- Found Lua ${LUA_VERSION_STRING}")
else()
  if(NOT DEFINED DEVILUTIONX_SYSTEM_LUA)
    message("-- Suitable system Lua package not found, will use Lua from source")
    set(DEVILUTIONX_SYSTEM_LUA OFF)
  endif()
endif()
dependency_options("lua" DEVILUTIONX_SYSTEM_LUA ON DEVILUTIONX_STATIC_LUA)
if(NOT DEVILUTIONX_SYSTEM_LUA)
  add_subdirectory(3rdParty/Lua)
  if(DEVILUTIONX_STATIC_LUA)
    set(LUA_LIBRARIES lua_static)
  else()
    set(LUA_LIBRARIES lua_shared)
  endif()
else()
  find_package(Lua 5.4 REQUIRED)
  include_directories(${LUA_INCLUDE_DIR})
endif()

add_subdirectory(3rdParty/sol2)

if(SCREEN_READER_INTEGRATION)
  if(WIN32)
    add_subdirectory(3rdParty/tolk)
  else()
    find_package(Speechd REQUIRED)
  endif()
endif()

if(EMSCRIPTEN)
  # We use `USE_PTHREADS=1` here to get a version of SDL2 that supports threads.
  emscripten_system_library("SDL2" SDL2::SDL2 USE_SDL=2 USE_PTHREADS=1)
  list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/SDL2/CMake")
elseif(USE_SDL1)
  find_package(SDL 1.2.10 REQUIRED)
  include_directories(${SDL_INCLUDE_DIR})
elseif(USE_SDL3)
  dependency_options("SDL3" DEVILUTIONX_SYSTEM_SDL3 ON DEVILUTIONX_STATIC_SDL3)
  if(DEVILUTIONX_SYSTEM_SDL3)
    find_package(SDL3 3.4.0 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
  else()
    add_subdirectory(3rdParty/SDL3)
  endif()
else()
  dependency_options("SDL2" DEVILUTIONX_SYSTEM_SDL2 ON DEVILUTIONX_STATIC_SDL2)
  if(DEVILUTIONX_SYSTEM_SDL2)
    find_package(SDL2 REQUIRED)
    if(TARGET SDL2::SDL2)
      if(TARGET SDL2::SDL2main)
        set(SDL2_MAIN SDL2::SDL2main)
      endif()
    elseif(TARGET SDL2::SDL2-static)
      # On some distros, such as vitasdk, only the SDL2::SDL2-static target is available.
      # Alias to SDL2::SDL2 because some finder scripts may refer to SDL2::SDL2.
      add_library(SDL2::SDL2 ALIAS SDL2::SDL2-static)
      if(TARGET SDL2::SDL2main)
        set(SDL2_MAIN SDL2::SDL2main)
      endif()
    else()
      # Assume an older Debian derivate that comes with an sdl2-config.cmake
      # that only defines `SDL2_LIBRARIES` (as -lSDL2) and `SDL2_INCLUDE_DIRS`.
      add_library(SDL2_lib INTERFACE)
      target_link_libraries(SDL2_lib INTERFACE ${SDL2_LIBRARIES})
      target_include_directories(SDL2_lib INTERFACE ${SDL2_INCLUDE_DIRS})
      # Can't define an INTERFACE target with ::, so alias instead
      add_library(SDL2::SDL2 ALIAS SDL2_lib)
    endif()
  elseif(UWP_LIB)
    list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/SDL2/CMake")
    find_package(SDL2 REQUIRED)
  else()
    add_subdirectory(3rdParty/SDL2)
    if(TARGET SDL2::SDL2main)
      set(SDL2_MAIN SDL2::SDL2main)
    endif()
    list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/SDL2/CMake")
  endif()
endif()

add_library(DevilutionX::SDL INTERFACE IMPORTED GLOBAL)
if(USE_SDL1)
  target_link_libraries(DevilutionX::SDL INTERFACE ${SDL_LIBRARY})
  target_compile_definitions(DevilutionX::SDL INTERFACE USE_SDL1)
elseif(USE_SDL3)
  target_link_libraries(DevilutionX::SDL INTERFACE SDL3::SDL3)
  target_compile_definitions(DevilutionX::SDL INTERFACE USE_SDL3)
else()
  if(TARGET SDL2::SDL2 AND NOT (DEVILUTIONX_STATIC_SDL2 AND TARGET SDL2::SDL2-static))
    target_link_libraries(DevilutionX::SDL INTERFACE SDL2::SDL2)
  elseif(TARGET SDL2::SDL2-static)
    target_link_libraries(DevilutionX::SDL INTERFACE SDL2::SDL2-static)
  endif()
  if(NOT UWP_LIB)
    target_link_libraries(DevilutionX::SDL INTERFACE ${SDL2_MAIN})
  endif()
endif()

macro(_find_SDL_image QUIET_OR_REQUIRED)
  # vcpkg uses sdl2-image as the package name
  find_package(sdl2-image QUIET)
  set(SDL_image_FOUND ${sdl2-image_FOUND})

  if(NOT SDL_image_FOUND)
    # Fall back on PkgConfig via FindSDL2_image.cmake
    find_package(SDL2_image ${QUIET_OR_REQUIRED})
    set(SDL_image_FOUND ${SDL2_image_FOUND})
  endif()
endmacro()

if(USE_SDL1)
  # No need for SDL_image.
elseif(USE_SDL3)
  if(NOT DEFINED DEVILUTIONX_SYSTEM_SDL_IMAGE)
    find_package(SDL3_image QUIET CONFIG)
    if(SDL3_image_FOUND)
      message("-- Found SDL3_image")
    else()
      message("-- Suitable system SDL3_image package not found, will use SDL3_image from source")
      set(DEVILUTIONX_SYSTEM_SDL_IMAGE OFF)
    endif()
  endif()
  dependency_options("SDL3_image" DEVILUTIONX_SYSTEM_SDL_IMAGE ON DEVILUTIONX_STATIC_SDL_IMAGE)
  if(DEVILUTIONX_SYSTEM_SDL_IMAGE)
    find_package(SDL3_image REQUIRED CONFIG)
  else()
    # Must be the same, see:
    # https://github.com/libsdl-org/SDL_image/blob/11154afb7855293159588b245b446a4ef09e574f/CMakeLists.txt#L225-L233
    set(DEVILUTIONX_STATIC_SDL_IMAGE ${DEVILUTIONX_STATIC_SDL3})
    add_subdirectory(3rdParty/SDL3_image)
  endif()
else()
  if(EMSCRIPTEN)
    emscripten_system_library("SDL_image" SDL2::SDL2_image USE_SDL_IMAGE=2 "SDL2_IMAGE_FORMATS='[\"png\"]'")
  else()
    if(NOT DEFINED DEVILUTIONX_SYSTEM_SDL_IMAGE)
      _find_SDL_image(QUIET)
      if(SDL_image_FOUND)
        message("-- Found SDL_image")
      else()
        message("-- Suitable system SDL_image package not found, will use SDL_image from source")
        set(DEVILUTIONX_SYSTEM_SDL_IMAGE OFF)
      endif()
    endif()
    dependency_options("SDL_image" DEVILUTIONX_SYSTEM_SDL_IMAGE ON DEVILUTIONX_STATIC_SDL_IMAGE)
    if(DEVILUTIONX_SYSTEM_SDL_IMAGE)
      _find_SDL_image(REQUIRED)
    else()
      add_subdirectory(3rdParty/SDL_image)
    endif()
  endif()
endif()

if(NOT DEFINED DEVILUTIONX_SYSTEM_LIBFMT)
  find_package(fmt 8.0.0 QUIET)
  if(fmt_FOUND)
    message("-- Found fmt ${fmt_VERSION}")
  else()
    message("-- Suitable system fmt package not found, will use fmt from source")
    set(DEVILUTIONX_SYSTEM_LIBFMT OFF)
  endif()
endif()
dependency_options("libfmt" DEVILUTIONX_SYSTEM_LIBFMT ON DEVILUTIONX_STATIC_LIBFMT)
if(DEVILUTIONX_SYSTEM_LIBFMT)
  find_package(fmt 8.0.0 REQUIRED)
else()
  add_subdirectory(3rdParty/libfmt)
endif()

if(NOT NOSOUND)
  if(USE_SDL3)
    if(NOT DEFINED DEVILUTIONX_SYSTEM_SDL_MIXER)
      find_package(SDL3_mixer QUIET CONFIG)
      if(SDL3_mixer_FOUND)
        message("-- Found SDL3_mixer")
      else()
        message("-- Suitable system SDL3_mixer package not found, will use SDL3_mixer from source")
        set(DEVILUTIONX_SYSTEM_SDL_MIXER OFF)
      endif()
    endif()
    dependency_options("SDL3_mixer" DEVILUTIONX_SYSTEM_SDL_MIXER ON DEVILUTIONX_STATIC_SDL_MIXER)
    if(DEVILUTIONX_SYSTEM_SDL_MIXER)
      find_package(SDL3_mixer REQUIRED CONFIG)
    else()
      add_subdirectory(3rdParty/SDL3_mixer)
    endif()
  else()
    if(NOT DEFINED DEVILUTIONX_SYSTEM_SDL_AUDIOLIB)
      find_package(SDL_audiolib QUIET)
      if(SDL_audiolib_FOUND)
        message("-- Found SDL_audiolib")
      else()
        message("-- Suitable system SDL_audiolib package not found, will use SDL_audiolib from source")
        set(DEVILUTIONX_SYSTEM_SDL_AUDIOLIB OFF)
      endif()
    endif()
    dependency_options("SDL_audiolib" DEVILUTIONX_SYSTEM_SDL_AUDIOLIB ON DEVILUTIONX_STATIC_SDL_AUDIOLIB)
    if(DEVILUTIONX_SYSTEM_SDL_AUDIOLIB)
      find_package(SDL_audiolib REQUIRED)
    else()
      add_subdirectory(3rdParty/SDL_audiolib)
    endif()
  endif()
endif()

if(PACKET_ENCRYPTION)
  dependency_options("libsodium" DEVILUTIONX_SYSTEM_LIBSODIUM ON DEVILUTIONX_STATIC_LIBSODIUM)
  if(DEVILUTIONX_SYSTEM_LIBSODIUM)
    set(sodium_USE_STATIC_LIBS ${DEVILUTIONX_STATIC_LIBSODIUM})
    find_package(sodium REQUIRED)
  else()
    add_subdirectory(3rdParty/libsodium)
  endif()
endif()

add_subdirectory(3rdParty/libsmackerdec)

if(WIN32 AND NOT UWP_LIB)
  add_subdirectory(3rdParty/find_steam_game)
endif()

if(SUPPORTS_MPQ)
  add_subdirectory(3rdParty/libmpq)
endif()

add_subdirectory(3rdParty/tl)

if(NOT DEFINED DEVILUTIONX_SYSTEM_SHEENBIDI)
  find_package(SheenBidi 2.9.0 QUIET)
  if(SheenBidi_FOUND)
    message("-- Found SheenBidi ${SheenBidi_VERSION}")
  else()
    message("-- Suitable system SheenBidi package not found, will use SheenBidi from source")
    set(DEVILUTIONX_SYSTEM_SHEENBIDI OFF)
  endif()
endif()
dependency_options("SheenBidi" DEVILUTIONX_SYSTEM_SHEENBIDI ON DEVILUTIONX_STATIC_SHEENBIDI)
if(DEVILUTIONX_SYSTEM_SHEENBIDI)
  find_package(SheenBidi REQUIRED)
else()
  add_subdirectory(3rdParty/SheenBidi)
endif()

if(NOT DEFINED DEVILUTIONX_SYSTEM_UNORDERED_DENSE)
  find_package(unordered_dense CONFIG QUIET)
  if (unordered_dense_FOUND)
    message("-- Found unordered_dense")
  else()
    message("-- Suitable unordered_dense package not found, will use unordered_dense from source")
    set(DEVILUTIONX_SYSTEM_UNORDERED_DENSE OFF)
  endif()
  dependency_options("unordered_dense" DEVILUTIONX_SYSTEM_UNORDERED_DENSE ON DEVILUTIONX_STATIC_UNORDERED_DENSE)
endif()
if(DEVILUTIONX_SYSTEM_UNORDERED_DENSE)
  find_package(unordered_dense CONFIG REQUIRED)
else()
  add_subdirectory(3rdParty/unordered_dense)
endif()

if(NOT DEFINED DEVILUTIONX_SYSTEM_MAGIC_ENUM)
  find_package(magic_enum 0.9.7 QUIET)
  if(magic_enum_FOUND)
    message("-- Found magic_enum ${magic_enum_VERSION}")
  else()
    message("-- Suitable system magic_enum package not found, will use magic_enum from source")
    set(DEVILUTIONX_SYSTEM_MAGIC_ENUM OFF)
  endif()
endif()
dependency_options("magic_enum" DEVILUTIONX_SYSTEM_MAGIC_ENUM ON DEVILUTIONX_STATIC_MAGIC_ENUM)
if(DEVILUTIONX_SYSTEM_MAGIC_ENUM)
  find_package(magic_enum REQUIRED)
else()
  add_subdirectory(3rdParty/magic_enum)
endif()

if(SUPPORTS_MPQ OR NOT NONET)
  add_subdirectory(3rdParty/PKWare)
endif()

if(NOT NONET AND NOT DISABLE_TCP)
  add_subdirectory(3rdParty/asio)
endif()

if(NOT NONET AND NOT DISABLE_ZERO_TIER)
  add_subdirectory(3rdParty/libzt)
endif()

if(DISCORD_INTEGRATION)
  add_subdirectory(3rdParty/discord)
endif()

if(BUILD_TESTING)
  dependency_options("googletest" DEVILUTIONX_SYSTEM_GOOGLETEST ON DEVILUTIONX_STATIC_GOOGLETEST)
  if(DEVILUTIONX_SYSTEM_GOOGLETEST)
    find_package(GTest REQUIRED)
    if(NOT TARGET GTest::gtest)
      add_library(GTest::gtest ALIAS GTest::GTest)
    endif()
  else()
    add_subdirectory(3rdParty/googletest)
  endif()

  dependency_options("benchmark" DEVILUTIONX_SYSTEM_BENCHMARK ON DEVILUTIONX_STATIC_BENCHMARK)
  if(DEVILUTIONX_SYSTEM_BENCHMARK)
    find_package(benchmark REQUIRED)
  else()
    add_subdirectory(3rdParty/benchmark)
  endif()
endif()

if(GPERF)
  find_package(Gperftools REQUIRED)
  message("INFO: ${GPERFTOOLS_LIBRARIES}")
endif()
